import {message} from 'antd';
import {formatMessage} from './intl';
import {partClaimReasonStatus} from '../Enum';

export const newOrUpdateValidator = (data, details) => {
    const emptyErrors = [];
    if(!data || !data.brandId)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.brandId',
            defaultMessage: '品牌'
        }));
    if(!data || !data.partInvoiceId)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.partInvoiceId',
            defaultMessage: '发票号'
        }));
    if(!data || !data.claimType)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.claimType',
            defaultMessage: '索赔类型'
        }));
    if(!data || !data.receiptDate)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.receiptDate',
            defaultMessage: '收货日期'
        }));
    if(emptyErrors.length > 0) {
        message.warning(`${emptyErrors.join('，')}${formatMessage({
            id: 'validator.required',
            defaultMessage: '必填!'
        })}`);
        return false;
    }
    if(data && data.remark && data.remark.length > 100) {
        message.warning(`${emptyErrors.join('，')}${formatMessage({
            id: 'validator.empty.remark',
            defaultMessage: '备注长度不能超过100个字'
        })}`);
        return false;
    }
    if(details.length <= 0) {
        message.warning(`${emptyErrors.join('，')}${formatMessage({
            id: 'validator.detailRequired',
            defaultMessage: '清单不能为空'
        })}`);
        return false;
    }
    if(details.some(d => !d.claimAmount || d.claimAmount <= 0)) {
        message.warning(formatMessage({
            id: 'validator.message.claimAmount',
            defaultMessage: '索赔数量不能为0！'
        }));
        return false;
    }
    if(details.some(d => d.claimReason !== partClaimReasonStatus.丢失))
        if(details.some(d => !d.attachments || d.attachments.length < 3)) {
            message.warning(formatMessage({
                id: 'validator.message.attachments',
                defaultMessage: '附件照片不能小于3张！'
            }));
            return false;
        }
    
    if(details.some(d => !d.claimReason)) {
        message.warning(formatMessage({
            id: 'validator.message.claimReason',
            defaultMessage: '索赔原因必填！'
        }));
        return false;
    }
    return true;
};

export const approveValidator = data => {
    const emptyErrors = [];
    if(!data || !data.paymentMethod)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.paymentMethod',
            defaultMessage: '赔付方式'
        }));
    if(!data || !data.approvedContent)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.approvedContent',
            defaultMessage: '审核意见'
        }));
    if(emptyErrors.length > 0) {
        message.warning(`${emptyErrors.join('，')}${formatMessage({
            id: 'validator.required',
            defaultMessage: '必填!'
        })}`);
        return false;
    }
    return true;
};
